<#assign className = table.className>   
<#assign classNameLower = className?uncap_first> 

package ${basepackage}.${namespace}.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.binaryhunter.framework.utils.MapConverter;

import pers.binaryhunter.framework.controller.GenericController;
import pers.binaryhunter.framework.exception.BusinessException;
import pers.binaryhunter.framework.bean.dto.paging.Page;
import pers.binaryhunter.framework.bean.po.PO;
import pers.binaryhunter.framework.bean.vo.paging.PageResult;

import ${basepackage}.${namespace}.bean.po.${className};
import ${basepackage}.${namespace}.service.${className}Service;

/**
 * ${table.tableAlias} Controller
 * @author Liyw
 */
@Controller
@RequestMapping("/${prjName}/${namespace}/${classNameLower}")
public class ${className}Controller extends GenericController {
	@Resource
	private ${className}Service ${classNameLower}Service;

	@ResponseBody
	@RequestMapping("/retrieve")
	public Object retrieve(${className} ${classNameLower}, Page page) {
		try {
            Map<String, Object> params = MapConverter.convertByField(${classNameLower});
            params.put("status", PO.STATUS_ENABLE);
            PageResult<${className}> pageResult = ${classNameLower}Service.pageByArgs(params, page);
            return toResponse(pageResult);
		} catch (Exception e) {
			return toResponse(e);
		}
	}
	
	@ResponseBody
	@RequestMapping("/create")
	public Object create(${className} ${classNameLower}) {
		try {
			if(null == ${classNameLower}) {
				throw new BusinessException();
			}
			${classNameLower}Service.add(${classNameLower});
			${classNameLower} = ${classNameLower}Service.getById(${classNameLower}.getId());
			return toResponse(${classNameLower});
		} catch (Exception e) {
			return toResponse(e);
		}
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public Object update(${className} ${classNameLower}) {
		try {
			if(null ==  ${classNameLower}.getId() || ${classNameLower}.getId() <= 0) {
				throw new BusinessException();
			}
			${classNameLower}Service.update(${classNameLower});
			${classNameLower} = ${classNameLower}Service.getById(${classNameLower}.getId());
			
			return toResponse(${classNameLower});
		} catch (Exception e) {
			return toResponse(e);
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Object delete(Long[] ids) {
		try {			
			if(null == ids || ids.length <= 0) {
				throw new BusinessException();
			}

            Map<String, Object> params = new HashMap<>();
            params.put("idIn", StringUtils.join(ids, ","));
			
			${classNameLower}Service.updateByArgs("t.status = " + PO.STATUS_DISABLE, params);
			
			return toResponse(ids);
		} catch (Exception e) {
			return toResponse(e);
		}
	}
	
	@ResponseBody
	@RequestMapping("/get")
	public Object get(Long id) {
		try {
			if(null == id || id <= 0) {
				throw new BusinessException();
			}

			${className} ${classNameLower} = ${classNameLower}Service.getById(id);

			return toResponse(${classNameLower});
		} catch (Exception e) {
			return toResponse(e);
		}
	}

}
